package com.xinghanlaser.applet.points;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xinghanlaser.applet.base.common.exception.ServiceException;
import com.xinghanlaser.applet.base.flow.core.BasePoint;
import com.xinghanlaser.applet.entity.TopicEvaluate;
import com.xinghanlaser.applet.entity.TopicHandleDetail;
import com.xinghanlaser.applet.service.ITopicEvaluateService;
import com.xinghanlaser.applet.service.ITopicHandleDetailService;
import com.xinghanlaser.applet.service.ITopicHandleHistoryService;
import com.xinghanlaser.applet.service.ITopicService;
import com.xinghanlaser.applet.vo.point.EvaluatePostVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *
 * <p>
 *
 * @author: mftuo 陀铭峰
 * @since: 2024/5/13 13:48
 */
@Component
public class DealerEvaluatePoint extends BasePoint<EvaluatePostVO> {
    @Resource
    private ITopicService topicService;

    @Resource
    private ITopicHandleDetailService handleService;

    @Resource
    private ITopicHandleHistoryService historyService;

    @Resource
    private ITopicEvaluateService evaluateService;

    @Override
    @DSTransactional
    protected void pointOperation(EvaluatePostVO vo) {
        final Long topicId = vo.getTopicId();

        topicService.validatedTopicId(topicId);

        // 更新处理流程信息
        TopicHandleDetail handleEntity = topicService.getHandleEntityByTopicId(topicId);
        handleEntity.setCurrentPointId(getPointId());
        handleService.updateById(handleEntity);

        // 保存处理流程历史信息
        Map<String , String > handlerDescMap = new HashMap<>();
        putGrade("grade1", vo.getGrade1(), handlerDescMap);
        putGrade("grade2", vo.getGrade2(), handlerDescMap);
        putGrade("grade3", vo.getGrade3(), handlerDescMap);
        putGrade("grade4", vo.getGrade4(), handlerDescMap);
        putGrade("grade5", vo.getGrade5(), handlerDescMap);

        try {
            // 保存评价信息
            TopicEvaluate entity = new TopicEvaluate();
            ObjectMapper objectMapper = new ObjectMapper();
            entity.setEvaluateDesc(vo.getDesc());
            entity.setGrade(objectMapper.writeValueAsString(handlerDescMap));
            evaluateService.save(entity);

            handlerDescMap.put("desc", vo.getDesc());
            String jsonString = objectMapper.writeValueAsString(handlerDescMap);

            // 保存历史记录
            historyService.saveHistory(
                    handleEntity.getId(),
                    getPointId(),
                    "经销商已评价",
                    jsonString
            );

        } catch (IOException e) {
            throw new ServiceException("转化json服务异常");
        }
    }

    @Override
    protected Class<EvaluatePostVO> getPointDataType() {
        return EvaluatePostVO.class;
    }

    @Override
    public Long getPointId() {
        return 10L;
    }

    private void putGrade(String gradeKey, Integer grade, Map<String , String > handlerDescMap){
        if(grade != null){
            handlerDescMap.put(gradeKey, grade.toString());
        }
    }
}
